package com.itguigu.glimail.order.listener;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.itguigu.glimail.order.config.AlipayTemplate;
import com.itguigu.glimail.order.service.OrderService;
import com.itguigu.glimail.order.service.OrderSettingService;
import com.itguigu.glimail.order.vo.PayAsyncVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
public class OrderPayedListener {
    /**
     * listen
     * 80;
     * server_ name gulimall.com *.gulimall.com 497n86m7k7. 52http.net;
     * #charset koi8-r;
     * #access_ log /wvar/log/nginx/log/host. access log main;
     * location /static/ {
     * root /usr/share/nginx/html;
     * location /payed/ {
     * proxy_ set_ header Host order.gulimall.com;
     * proxy_ pass http://gulimall;
     *
     * @return
     */
    @Autowired
    OrderService orderService;
    @Autowired
    AlipayTemplate alipayTemplate;
    @PostMapping("payed/notify")
    public String handleAlipayed(PayAsyncVo payAsyncVo,HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
        Map<String, String[]> parameterMap =
                request.getParameterMap();
        for (String s : parameterMap.keySet()) {
            String parameter = request.getParameter(s);
            System.out.println(s + "======>" + parameter);
        }
        ////只要 我们收到了支付宝给我们异步的通知，告诉我们订单支付成功。返回success,支付宝就再也不通知
//验签
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayTemplate.getAlipay_public_key(), alipayTemplate.getCharset(), alipayTemplate.getSign_type()); //调用SDK验证签名
        if (signVerified) {
            System.out.println("前面验证成功");
            String result = orderService.handlePayResult(payAsyncVo);
            return result;
        }

        return "error";
    }
}
